#include <map>
#include <vector>
#include <string>
#include "smartpipe.h"

using namespace std;
using namespace sp;

void registe(){
    Image::Crop::cropWithInput::registe();
    Image::Crop::cropWithParas::registe();
    Image::Gen::genFromDisk::registe();
    Image::Gen::genFromMemory::registe();
    Image::Resize::letterBoxResize::registe();
    Image::Resize::resize::registe();
    Image::Save::markAndSave::registe();
    Image::Save::save::registe();
    Image::Trans::trans::registe();
    Model::Group::groupByBatch::registe();
    Model::Split::splitByShape::registe();
    Model::Trans::transferToDeviceMemory::registe();
    Model::Trans::transferToHostMemory::registe();
    Model::Yolo::yolo_complete::registe();
    Model::Yolo::yolo_preprocess::registe();
    Model::Yolo::yolo_inference::registe();
    Model::Yolo::yolo_postprocess::registe();
    Model::Retinanet::retinanet_preprocess::registe();
    Model::Retinanet::retinanet_inference::registe();
    Model::Retinanet::retinanet_postprocess::registe();
    Model::LPRnet::lprnet_preprocess::registe();
    Model::LPRnet::lprnet_inference::registe();
    Model::LPRnet::lprnet_postprocess::registe();
    Model::Openpose::openpose_preprocess::registe();
    Model::Openpose::openpose_inference::registe();
    Model::Openpose::openpose_postprocess::registe();
    Tool::Group::groupByRequestId::registe();
    Tool::Split::splitByFlowId::registe();
}

char* DATA_SOURCE;
char* DATA_TARGET;

char* input_video_data_ptr;
char* output_video_data_ptr;

// 共用参数
string video_path = "/data/lx/SmartPipe/data_source/videos/0123.mp4";                   // 视频路径
string save_path = "/data/lx/SmartPipe/apps/car_license_plate_recognition/output.avi";  // 输出路径
int video_channels = 4;                // 视频路数
long single_cnt = 600;                 // 单路视频帧数
int single_fps = 30;                   // 单路视频帧率
long cnt = single_cnt*video_channels;  // 处理帧数

/** scale = 0.5
    [0, "f0", "1-genFromMemory", 32483.438],
    [1, "f1", "2-resize", 21615.085],
    [2, "f2_0", "3-yolo_preprocess", 21185.06],
    [2, "f2_1", "3-yolo_preprocess", 21185.06],
    [3, "f3_0", "4-groupByBatch", 18267.064],
    [4, "f3_1", "4-groupByBatch", 18267.064],
    [5, "f4", "5-transferToDeviceMemory", 5836.084],
    [8, "f6", "7-transferToHostMemory", 33333.000], // 58199.324
    [9, "f13", "14-transferToDeviceMemory", 2137.5],
    [10, "f15", "16-transferToHostMemory", 2826.949],
    [11, "f22", "23-transferToDeviceMemory", 711.916],
    [12, "f24", "25-transferToHostMemory", 481.507],
    [13, "f5", "6-yolo_inference", 18775.318],
    [15, "f14", "15-retinanet_infernce", 5168.348],
    [16, "f23", "24-lprnet_inference", 714.508],
    [17, "f7_0", "8-splitByShape", 27620.127],
    [18, "f7_1", "8-splitByShape", 27620.127],
    [19, "f8_0", "9-yolo_postprocess", 23501.088],
    [20, "f8_1", "9-yolo_postprocess", 23501.088],
    [21, "f9", "10-cropWithInput", 6522.54],
    [22, "f10", "11-resize", 3814.932],
    [23, "f11", "12-retinanet_preprocess", 19961.254],
    [24, "f12", "13-groupByBatch", 12461.578],
    [25, "f16", "17-splitByShape", 2636.769],
    [26, "f17", "18-retinanet_postprocess", 11701.184],
    [27, "f18", "19-cropWithInput", 817.735],
    [28, "f19", "20-resize", 731.492],
    [29, "f20", "21-lprnet_preprocess", 434.177],
    [30, "f21", "22-groupByBatch", 443.915],
    [31, "f25", "26-splitByShape", 248.079],
    [32, "f26", "27-lprnet_postprocess", 656.516],
    [33, "f27", "28-groupByRequestId", 250.546],
    [34, "f28", "29-markAndSave", 14104.68]
**/

void test_scale0_5(SharedMemoryManager& smm, Gpu_SharedMemoryManager& gpu_smm){
    Function* f0 = new Image::Gen::genFromMemory(input_video_data_ptr, cnt, single_fps*video_channels, 3840, 2160);
    Function* f1 = new Image::Resize::resize(640, 384);                 // * 2
    Function* f2 = new Model::Yolo::yolo_preprocess(640, 384);          // * 2
    Function* f3 = new Model::Group::groupByBatch(8);                   // * 2
    Function* f4 = new Model::Trans::transferToDeviceMemory();
    Function* f5 = new Model::Yolo::yolo_inference(640, 384);
    Function* f6 = new Model::Trans::transferToHostMemory();
    Function* f7 = new Model::Split::splitByShape();                    // * 2
    Function* f8_0 = new Model::Yolo::yolo_postprocess(640, 384);         // * 4
    Function* f8_1 = new Model::Yolo::yolo_postprocess(640, 384);         // * 4
    Function* f9 = new Image::Crop::cropWithInput(0);
    Function* f10 = new Image::Resize::resize(320, 320);
    Function* f11 = new Model::Retinanet::retinanet_preprocess();
    Function* f12 = new Model::Group::groupByBatch(16);
    Function* f13 = new Model::Trans::transferToDeviceMemory();
    Function* f14 = new Model::Retinanet::retinanet_inference();
    Function* f15 = new Model::Trans::transferToHostMemory();
    Function* f16 = new Model::Split::splitByShape();
    Function* f17 = new Model::Retinanet::retinanet_postprocess();
    Function* f18 = new Image::Crop::cropWithInput(1);
    Function* f19 = new Image::Resize::resize(94, 24);
    Function* f20 = new Model::LPRnet::lprnet_preprocess();
    Function* f21 = new Model::Group::groupByBatch(16);
    Function* f22 = new Model::Trans::transferToDeviceMemory();
    Function* f23 = new Model::LPRnet::lprnet_inference();
    Function* f24 = new Model::Trans::transferToHostMemory();
    Function* f25 = new Model::Split::splitByShape();
    Function* f26 = new Model::LPRnet::lprnet_postprocess();
    Function* f27 = new Tool::Group::groupByRequestId(cnt);
    Function* f28 = new Image::Save::markAndSave(output_video_data_ptr, 30, 3840, 2160);
    // 连接逻辑关系
    // 其他前置连接
    connectOneToMany(3, f0, f9, f28);
    // 一些前置连接
    connect(f10, f18);
    // 前置连接
    // connectOneByOne(4, f0, f1, f2, f3);
    // 两路分别相连
    // connectOneByOne(3, f4_0, f5_0, f6_0);
    // connectOneByOne(3, f4_1, f5_1, f6_1);
    // 后置连接
    connectOneByOne(8, f0, f1, f2, f3, f4, f5, f6, f7);
    connectOneByOne(20, f9, f10, f11, f12, f13, f14, f15, f16, f17, f18, f19, f20, f21, f22, f23, f24, f25, f26, f27, f28);
    // 相连共同部分
    connectOneFanMany(3, f7, f8_0, f8_1);
    connectManyCollectOne(3, f8_0, f8_1, f9);
    // 部署表
    map<short, vector<Function*>> Fs_map;
    Fs_map[0] = {f5, f14, f23, f10};                                                                  // GPU:0
    Fs_map[1] = {f4, f6, f13, f15, f22, f24};
    Fs_map[2] = {f0};
    Fs_map[3] = {f1, f17};      
    Fs_map[4] = {f2, f16};       
    Fs_map[5] = {f3, f9, f18, f19, f20, f21, f25, f26, f27, f28};      
    Fs_map[6] = {f7};
    Fs_map[7] = {f8_0, f11};
    Fs_map[8] = {f8_1, f12};
    // 构造app
    App app(0, Fs_map, &smm, &gpu_smm); // 最后一个参数代表是否进行核的绑定
    // 运行app
    app.check();
    app.printMsg();
    app.init();
    app.run();
    app.waitForComplete();
}

/** scale = 0.5
    [0, "f0", "1-genFromMemory", 32483.438],
    [1, "f1", "2-resize", 21615.085],
    [2, "f2_0", "3-yolo_preprocess", 21185.06],
    [2, "f2_1", "3-yolo_preprocess", 21185.06],
    [3, "f3_0", "4-groupByBatch", 18267.064],
    [4, "f3_1", "4-groupByBatch", 18267.064],
    [5, "f4", "5-transferToDeviceMemory", 5836.084],
    [8, "f6", "7-transferToHostMemory", 33333.000], // 58199.324
    [9, "f13", "14-transferToDeviceMemory", 2137.5],
    [10, "f15", "16-transferToHostMemory", 2826.949],
    [11, "f22", "23-transferToDeviceMemory", 711.916],
    [12, "f24", "25-transferToHostMemory", 481.507],
    [13, "f5", "6-yolo_inference", 7283.529],
    [15, "f14", "15-retinanet_infernce", 705.13],
    [16, "f23", "24-lprnet_inference", 222.94],
    [17, "f7_0", "8-splitByShape", 27620.127],
    [18, "f7_1", "8-splitByShape", 27620.127],
    [19, "f8_0", "9-yolo_postprocess", 23501.088],
    [20, "f8_1", "9-yolo_postprocess", 23501.088],
    [21, "f9", "10-cropWithInput", 6522.54],
    [22, "f10", "11-resize", 3814.932],
    [23, "f11", "12-retinanet_preprocess", 19961.254],
    [24, "f12", "13-groupByBatch", 12461.578],
    [25, "f16", "17-splitByShape", 2636.769],
    [26, "f17", "18-retinanet_postprocess", 11701.184],
    [27, "f18", "19-cropWithInput", 817.735],
    [28, "f19", "20-resize", 731.492],
    [29, "f20", "21-lprnet_preprocess", 434.177],
    [30, "f21", "22-groupByBatch", 443.915],
    [31, "f25", "26-splitByShape", 248.079],
    [32, "f26", "27-lprnet_postprocess", 656.516],
    [33, "f27", "28-groupByRequestId", 250.546],
    [34, "f28", "29-markAndSave", 14104.68]
**/

void test_scale1(SharedMemoryManager& smm, Gpu_SharedMemoryManager& gpu_smm){
    // 构造app
    Function* f0 = new Image::Gen::genFromMemory(input_video_data_ptr, cnt, single_fps*video_channels, 3840, 2160);
    Function* f1 = new Image::Resize::resize(640, 384);
    Function* f2 = new Model::Yolo::yolo_preprocess(640, 384);
    Function* f3 = new Model::Group::groupByBatch(32);
    Function* f4 = new Model::Trans::transferToDeviceMemory();
    Function* f5 = new Model::Yolo::yolo_inference(640, 384);
    Function* f6 = new Model::Trans::transferToHostMemory();
    Function* f7_0 = new Model::Split::splitByShape();
    Function* f7_1 = new Model::Split::splitByShape();
    Function* f8_0 = new Model::Yolo::yolo_postprocess(640, 384);
    Function* f8_1 = new Model::Yolo::yolo_postprocess(640, 384);
    Function* f8_2 = new Model::Yolo::yolo_postprocess(640, 384);
    Function* f8_3 = new Model::Yolo::yolo_postprocess(640, 384);
    Function* f9 = new Image::Crop::cropWithInput(0);
    Function* f10 = new Image::Resize::resize(320, 320);
    Function* f11 = new Model::Retinanet::retinanet_preprocess();
    Function* f12 = new Model::Group::groupByBatch(128);
    Function* f13 = new Model::Trans::transferToDeviceMemory();
    Function* f14 = new Model::Retinanet::retinanet_inference();
    Function* f15 = new Model::Trans::transferToHostMemory();
    Function* f16 = new Model::Split::splitByShape();
    Function* f17 = new Model::Retinanet::retinanet_postprocess();
    Function* f18 = new Image::Crop::cropWithInput(1);
    Function* f19 = new Image::Resize::resize(94, 24);
    Function* f20 = new Model::LPRnet::lprnet_preprocess();
    Function* f21 = new Model::Group::groupByBatch(256);
    Function* f22 = new Model::Trans::transferToDeviceMemory();
    Function* f23 = new Model::LPRnet::lprnet_inference();
    Function* f24 = new Model::Trans::transferToHostMemory();
    Function* f25 = new Model::Split::splitByShape();
    Function* f26 = new Model::LPRnet::lprnet_postprocess();
    Function* f27 = new Tool::Group::groupByRequestId(cnt);
    Function* f28 = new Image::Save::markAndSave(output_video_data_ptr, 30, 3840, 2160);
    // 连接逻辑关系
    // 其他前置连接
    connectOneToMany(3, f0, f9, f28);
    // 一些前置连接
    connect(f10, f18);
    // 前置连接
    // connectOneByOne(4, f0, f1, f2, f3);
    // 两路分别相连
    // connectOneByOne(3, f4_0, f5_0, f6_0);
    // connectOneByOne(3, f4_1, f5_1, f6_1);
    // 后置连接
    connectOneByOne(7, f0, f1, f2, f3, f4, f5, f6);
    connectOneByOne(20, f9, f10, f11, f12, f13, f14, f15, f16, f17, f18, f19, f20, f21, f22, f23, f24, f25, f26, f27, f28);
    // 相连共同部分
    // connectOneFanMany(3, f3, f4_0, f4_1);
    // connectManyCollectOne(3, f6_0, f6_1, f7);
    connectOneFanMany(3, f6, f7_0, f7_1);
    connectOneFanMany(3, f7_0, f8_0, f8_1);
    connectOneFanMany(3, f7_1, f8_2, f8_3);
    connectManyCollectOne(5, f8_0, f8_1, f8_2, f8_3, f9);
    // 部署表
    map<short, vector<Function*>> Fs_map;
    Fs_map[0] = {f5, f14, f23};                                              // GPU:0
    Fs_map[1] = {f4, f6, f13, f15, f22, f24};                                // GPU:0
    Fs_map[2] = {f0};      
    Fs_map[3] = {f1};                   
    Fs_map[4] = {f2};                                  
    Fs_map[5] = {f3};                                        
    Fs_map[6] = {f7_0, f17};
    Fs_map[7] = {f7_1, f18, f19, f20, f21, f25, f26};
    Fs_map[8] = {f8_0, f9, f27};
    Fs_map[9] = {f8_1, f10, f16};
    Fs_map[10] = {f8_2, f11};
    Fs_map[11] = {f8_3, f12};
    Fs_map[12] = {f28};
    // 构造app
    App app(0, Fs_map, &smm, &gpu_smm); // 最后一个参数代表是否进行核的绑定
    // 运行app
    app.check();
    app.printMsg();
    app.init();
    app.run();
    app.waitForComplete();
}

int main(){
    // 注册
    registe();
    // 声明共享内存管理对象
    SharedMemoryManager smm;
    Gpu_SharedMemoryManager gpu_smm;
    // 将视频加载到内存中
    SharedMemoryPool input_video_pool = smm.createSharedMemoryPool("input_video", Cv_Mat_Data_3840_2160_Memory, cnt);
    SharedMemoryPool output_video_pool = smm.createSharedMemoryPool("output_video", Cv_Mat_Data_3840_2160_Memory, cnt);
    input_video_data_ptr = input_video_pool.getDataPtr();
    output_video_data_ptr = output_video_pool.getDataPtr();
    Image::Gen::genFromDisk f_start(video_path, input_video_data_ptr, cnt, 30, 3840, 2160);
    f_start.run();
    // 进行测试
    // pid_t pid = fork();
    // if(pid){
    //     int res = system("monitor lab1");
    // }else{
    test_scale0_5(smm, gpu_smm);
        // 检查是否存在内存泄露
    smm.check();
        // 将输出视频保存到磁盘
        // Image::Save::save f_end(save_path, output_video_data_ptr, cnt, 30, 3840, 2160);
        // f_end.run();
    // }
}